package com.gable.exhibition.thread;

import com.gable.common.util.StringUtil;
import com.gable.exhibition.dao.InstallDao;
import com.gable.exhibition.dao.UserDao;
import com.gable.exhibition.util.HttpHelper;
import com.gable.exhibition.view.UserView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mj on 2018/2/28.
 * 验收短信推送
 */
public class CheckSendSmsThread implements Runnable{
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private Long installId;

    private InstallDao installDao;

    private UserDao userDao;

    private String sendSmsForAcceptanceUrl;

    public CheckSendSmsThread(Long installId,InstallDao installDao,UserDao userDao,String sendSmsForAcceptanceUrl) {
        this.installId = installId;
        this.installDao = installDao;
        this.userDao = userDao;
        this.sendSmsForAcceptanceUrl = sendSmsForAcceptanceUrl;
    }

    @Override
    public void run() {
        try {
            Map<String, Object> installMap = installDao.checkForSms(installId);
            String hospitalName = installMap.get("hospitalName") + "";
            String deviceName = installMap.get("deviceName") + "";
            String serviceType = installMap.get("serviceType") + "";
            Long serviceId = Long.parseLong(installMap.get("serviceId")+"");
            UserView userView = new UserView();
            if("1103".equals(serviceType)){     //维修商
                userView.setMaintainerId(serviceId);
            }else if("1102".equals(serviceType)){      //厂商
                userView.setManufacturerId(serviceId);
            }else if("1105".equals(serviceType)){   //供应商
                userView.setSupplierId(serviceId);
            }
            List<String> usersMobile = userDao.getUsersMobile(userView);
            StringBuilder sb = new StringBuilder();
            for(String phone : usersMobile){
                if(StringUtil.isEmpty(phone))
                    continue;
                sb.append(phone).append(",");
            }
            String mobile = sb.substring(0, sb.length() - 1);
            String message = hospitalName+"%&"+deviceName;
            Map<String,String> hospitalMap = new HashMap<String,String>();
            hospitalMap.put("mobile",mobile);
            hospitalMap.put("message",message);
            String jsonResult = HttpHelper.postRequestResponseBodyAsString(sendSmsForAcceptanceUrl,hospitalMap);
            logger.info("验收完成推送给服务商的短信设备名称："+deviceName+",短信发送结果:"+jsonResult);
        } catch (Exception e) {
            logger.info("验收完成推送给服务商的短信发送失败:"+e);
        }
    }
}
